package com.didi.gulimall.member.controller;

import com.didi.gulimall.common.utils.PageUtils;
import com.didi.gulimall.common.utils.R;
import com.didi.gulimall.member.entity.MemberEntity;
import com.didi.gulimall.member.service.MemberService;
import com.didi.gulimall.member.vo.MemberLoginVo;
import com.didi.gulimall.member.vo.MemberOrderPointsVo;
import com.didi.gulimall.member.vo.MemberRegistVo;
import com.didi.gulimall.member.vo.SocialUser;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 会员
 *
 * @author didi
 * @email 1062664904@qq.com
 * @date 2021-01-14 16:07:50
 */
@RestController
@RequestMapping("member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }

    @PostMapping("subject/points")
    public R subjectPoint(@RequestBody List<MemberOrderPointsVo> memberOrderPointsVos, HttpSession httpSession){
        return memberService.subjectPoint(memberOrderPointsVos,httpSession);
    }
    /**
     * 注册
     */
    @ApiOperation("注册")
    @PostMapping("regist")
    public R registered(@RequestBody MemberRegistVo memberRegistVo) {
        return memberService.registered(memberRegistVo);
    }

    @ApiOperation("登录")
    @PostMapping("login")
    public R login(
            @ApiParam(name = "memberLoginVo", value = "用户登录Vo", required = true)
            @RequestBody MemberLoginVo memberLoginVo) {
        return memberService.login(memberLoginVo);
    }

    @ApiOperation("微博授权登录")
    @PostMapping("oauth/weibo/login")
    public R oauthweibologin(
            @ApiParam(name = "socialUser", value = "微博授权登录", required = true)
            @RequestBody SocialUser socialUser) throws Exception {
        return memberService.oauthweibologin(socialUser);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
